package com.lilicould.service;

import com.google.gson.Gson;
import com.lilicould.dao.StudentDao;

import com.lilicould.dao.UserDao;
import com.lilicould.entity.Students;
import com.lilicould.entity.StudentsDetails;

import com.lilicould.entity.Users;
import com.lilicould.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.mindrot.jbcrypt.BCrypt;

import java.util.List;

public class StudentService {
    // 获取一个学生信息
    public String getStudent(String userNo) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            // 创建SqlSession
            StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
            // 调用方法获取学生信息
            StudentsDetails studentsDetails = studentDao.getStudentDetailsByUserNo(userNo);
            // 创建Gson对象
            // 将结果转换为JSON格式并返回
            if (studentsDetails != null) {
                // 如果返回数据非空，说明成功获取到数据，则返回数据给前端
                return new Gson().toJson(studentsDetails);
            }else return null;
        } catch (Exception e) {
            return null;
        }
    }

    // 获取所有学生信息
    public String getAllStudents() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            // 创建SqlSession
            StudentDao studentDao = sqlSession.getMapper(StudentDao.class);

            // 调用方法获取所有学生信息
            List<StudentsDetails> studentsDetails = studentDao.getAllStudentsDetails();

            // 返回JSON格式数据
            Gson gson = new Gson();
            return gson.toJson(studentsDetails);
        } catch (Exception e) {
            // 捕获异常并打印错误信息
            System.out.println(e.getMessage());
            // 发生异常时返回null
            return null;
        }
        // 关闭SqlSession
    }

    // 添加一个学生信息
    public String addStudent(StudentsDetails studentsDetails) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            // 获取映射器对象
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            StudentDao studentDao = sqlSession.getMapper(StudentDao.class);

            // 添加用户
            Users user = new Users(studentsDetails.getUserNo(), BCrypt.hashpw("123456", BCrypt.gensalt()), "student", studentsDetails.getName(), null, null);
            Students student = new Students(studentsDetails.getUserNo(), studentsDetails.getEnrollmentYear(), studentsDetails.getCollegeCode(), studentsDetails.getMajorCode(), studentsDetails.getClassCode());
            int result = userDao.addUser(user);
            if (result > 0) {
                result = studentDao.addStudent(student);
                if (result > 0) {
                    sqlSession.commit();
                    return null;
                } else {
                    return "添加学生信息失败";
                }
            } else {
                return "添加用户信息失败";
            }
        } catch (Exception e) {
            return e.getMessage();
        }

    }

    // 删除一个学生信息
    public String deleteStudent(String userNo) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            // 获取映射器对象
            StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
            UserDao userDao = sqlSession.getMapper(UserDao.class);

            // 删除用户
            // 此处一定要先删除学生扩展信息，再删除学生信息，否则会报外键约束错误
            int result = studentDao.deleteStudent(userNo);
            if (result > 0) {
                result = userDao.deleteUser(userNo);
                if (result > 0) {
                    sqlSession.commit();
                    return null;
                }else
                    return "删除用户信息失败";
            }else {
                return "删除学生信息失败";
            }
        }catch (Exception e) {
            return e.getMessage();
        }
    }


}
